﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using System.Reflection;
using System.Data;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    public class DataQueryParamELBDao : DaoService<DataQueryParam>
    { 
        public override string TableName => "FAS_DataQueryParam";
        public override string IDColumnName => "ID";
        public string ForeignColumn => "DataQueryID";
        public override List<string> ExceptColumnName {get;} = DaoUtil.GetExceptionColumn<DataQueryParam>();

        //public override Dictionary<string, object> GetDataMapping(DataQueryParam data)
        //{
        //    var datas = base.GetDataMapping(data);
        //    datas.Remove(nameof(data.IsDefault));
        //    datas.Add(nameof(data.IsDefault), (bool)data.IsDefault);
        //    return datas;
        //}

        public override DataQueryParam ConvertToObject(Dictionary<string, PropertyInfo> columns, DataRow row)
        {
            var myCol = new Dictionary<string, PropertyInfo>(base.Columns);
            myCol.Remove("IsDefault");
            var data = base.ConvertToObject(myCol, row);
            //if (row["IsDefault"] != null)
            //    data.IsDefault = int.Parse(Convert.ToString(row["IsDefault"])) == 0 ? false : true;

            string isDefaultVal = row["IsDefault"]?.ToString();
            if (!string.IsNullOrEmpty(isDefaultVal))
            {
                if (int.TryParse(isDefaultVal, out int isDefaultInt))
                {
                    data.IsDefault = isDefaultInt == 0 ? false : true;
                }
                else
                    data.IsDefault = false;
            }
            else
                data.IsDefault = false;

            return data;
        }

        public DataQueryParamELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="dqId"></param>
        /// <returns></returns>
        public List<DataQueryParam> GetDataQueryParams(string dqId)
        {
            var result = new List<DataQueryParam>();
            if (string.IsNullOrEmpty(dqId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{dqId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as DataQueryParam));
            }
            return result;
        }

        /// <summary>
        /// 添加DataQueryParam
        /// </summary>
        /// <param name="dqParams"></param>
        public void CreateDataQueryParam(List<DataQueryParam> dqParams)
        {
            try
            {
                base.Add(dqParams);
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.CreateParamError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQueryParam
        /// </summary>
        /// <param name="dqId"></param>
        public void DeleteDataQueryParam(string dqId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteParamError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除DataQuerySource
        /// </summary>
        /// <param name="dqIds"></param>
        public void BatchDeleteDataQueryParam(List<string> dqIds)
        {
            try
            {
                List<string> sqls = new List<string>();
                foreach (string dqId in dqIds)
                {
                    sqls.Add(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, dqId));
                }
                base.DB.ExecSqlStatement(sqls.ToArray());
            }
            catch (Exception e)
            {
                throw new FasException(DataModelErrorCode.DQ.DeleteParamError, e.Message, e);
            }
        }
    }
}
